package Vista;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import Controlador.Sistema;


public class ViewAltaPlan extends javax.swing.JFrame {
	private JLabel jLNombrePlan;
	private JTextField jTNombrePlan;
	private JComboBox jComboBTipoPlan;
	private JTextField jTCosto;
	private JLabel lblCostoExcedente;
	private JCheckBox jCheckPaquete;
	private JComboBox jComboBPaquete;
	private JComboBox jComboBServicioVoz;
	private JComboBox jComboBServicioSms;
	private JComboBox jComboBServicioDatos;
	private JCheckBox jCheckDatos;
	private JCheckBox jCheckSms;
	private JCheckBox jCheckVoz;
	private JButton jBCancelar;
	private JButton jBguardar;
	private JLabel jLabel1;
	public static ViewAltaPlan instancia;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				ViewAltaPlan inst = new ViewAltaPlan();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	private ViewAltaPlan() {
		super();
		initGUI();
	}
	
	public static ViewAltaPlan getInstancia() 
	{
		if (instancia== null)
			instancia = new ViewAltaPlan();
		return instancia;
	}
		
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			{
				jLNombrePlan = new JLabel();
				getContentPane().add(jLNombrePlan);
				jLNombrePlan.setText("Nombre del Plan");
				jLNombrePlan.setBounds(23, 30, 97, 14);
			}
			{
				jTNombrePlan = new JTextField();
				getContentPane().add(jTNombrePlan);
				jTNombrePlan.setBounds(120, 27, 376, 21);
			}
			{
				ComboBoxModel jComboBTipoPlanModel = 
					new DefaultComboBoxModel(
							new String[] {"" , "BASE", "PREPAGO", "ADICIONAL" });
				jComboBTipoPlan = new JComboBox();
				getContentPane().add(jComboBTipoPlan);
				jComboBTipoPlan.setModel(jComboBTipoPlanModel);
				jComboBTipoPlan.setBounds(120, 63, 376, 21);
				jComboBTipoPlan.addItemListener(new ItemListener() {
					public void itemStateChanged(ItemEvent evt) {
						System.out.println("jComboBTipoPlan.itemStateChanged, event="+evt);
						String seleccion = (String) jComboBTipoPlan.getSelectedItem();
						if (seleccion=="BASE")
						{
							jCheckDatos.setEnabled(true);
							jCheckPaquete.setEnabled(true);
							jCheckSms.setEnabled(true);
							jCheckVoz.setEnabled(true);
							jTCosto.setEnabled(false);
													
						};
						if (seleccion=="PREPAGO"){
							jCheckDatos.setEnabled(false);
							jCheckPaquete.setEnabled(false);
							jCheckSms.setEnabled(true);
							jCheckVoz.setEnabled(true);
							jTCosto.setEnabled(false);
														
						}
						if (seleccion=="ADICIONAL"){
							jCheckDatos.setEnabled(true);
							jCheckPaquete.setEnabled(true);
							jCheckSms.setEnabled(true);
							jCheckVoz.setEnabled(true);
							jTCosto.setEnabled(true);

						};
						/*jCheckDatos.removeAll();
						jCheckPaquete.removeAll();
						jCheckSms.removeAll();
						jCheckVoz.removeAll();*/
					}				
				});
				
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Tipo Plan");
				jLabel1.setBounds(23, 66, 97, 14);
			}
			{
				jBguardar = new JButton();
				getContentPane().add(jBguardar);
				jBguardar.setText("GUARDAR");
				jBguardar.setBounds(54, 277, 90, 21);
				jBguardar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jBguardar.actionPerformed, event="+evt);
						String seleccionG = (String) jComboBTipoPlan.getSelectedItem();
						
						if (seleccionG == "BASE")
						{
							Vector<String> serviciosPlan = new Vector<String>(); 
							if(jCheckVoz.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioVoz.getSelectedItem());
					        }
							if(jCheckDatos.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioDatos.getSelectedItem());
					        }
							if(jCheckSms.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioSms.getSelectedItem());
					        }
							if(jCheckPaquete.isSelected())
					        {
								serviciosPlan.add((String) jComboBPaquete.getSelectedItem());
					        }
							
						    Sistema.getInstancia().altaPlan(jTNombrePlan.getText(), 0, seleccionG, serviciosPlan);
						    
						}
						else if (seleccionG == "PREPAGO")
						{
							Vector<String> serviciosPlan = new Vector<String>(); 
							if(jCheckVoz.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioVoz.getSelectedItem());
					        }
							if(jCheckDatos.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioDatos.getSelectedItem());
					        }
							if(jCheckSms.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioVoz.getSelectedItem());
					        }
							if(jCheckPaquete.isSelected())
					        {
								serviciosPlan.add((String) jComboBPaquete.getSelectedItem());
					        }
							
						    Sistema.getInstancia().altaPlan(jTNombrePlan.getText(), 0, seleccionG, serviciosPlan);
						}
						else if (seleccionG == "ADICIONAL")
						{
							Vector<String> serviciosPlan = new Vector<String>(); 
							if(jCheckVoz.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioVoz.getSelectedItem());
					        }
							if(jCheckDatos.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioDatos.getSelectedItem());
					        }
							if(jCheckSms.isSelected())
					        {
								serviciosPlan.add((String) jComboBServicioSms.getSelectedItem());
					        }
							if(jCheckPaquete.isSelected())
					        {
								serviciosPlan.add((String) jComboBPaquete.getSelectedItem());
					        }
							
						    Sistema.getInstancia().altaPlan(jTNombrePlan.getText(), Float.parseFloat(jTCosto.getText()), seleccionG, serviciosPlan);
						};													
					}
					}
					);				
			}
			{
				jBCancelar = new JButton();
				getContentPane().add(jBCancelar);
				jBCancelar.setText("CANCELAR");
				jBCancelar.setBounds(406, 277, 102, 21);
			}
			{
				jCheckVoz = new JCheckBox();
				getContentPane().add(jCheckVoz);
				jCheckVoz.setText("Voz");
				jCheckVoz.setBounds(18, 156, 97, 18);
				jCheckVoz.setEnabled(false);
				jCheckVoz.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jCheckVoz.actionPerformed, event="+evt);					
						if(jCheckVoz.isSelected())
				        {
							jComboBServicioVoz.setEnabled(true);
				        }
				        else jComboBServicioVoz.setEnabled(false);
					}
				});
			}
			{
				jCheckSms = new JCheckBox();
				getContentPane().add(jCheckSms);
				jCheckSms.setText("SMS");
				jCheckSms.setBounds(18, 191, 97, 18);
				jCheckSms.setEnabled(false);
				jCheckSms.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jCheckSms.actionPerformed, event="+evt);					
						if(jCheckSms.isSelected())
				        {
							jComboBServicioSms.setEnabled(true);
				        }
				        else jComboBServicioSms.setEnabled(false);
					}
				});
			}
			{
				jCheckDatos = new JCheckBox();
				getContentPane().add(jCheckDatos);
				jCheckDatos.setText("Datos");
				jCheckDatos.setBounds(18, 226, 97, 18);
				jCheckDatos.setEnabled(false);
				jCheckDatos.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jCheckDatos.actionPerformed, event="+evt);					
						if(jCheckDatos.isSelected())
				        {
							jComboBServicioDatos.setEnabled(true);
				        }
				        else jComboBServicioDatos.setEnabled(false);
					}
				});
			}
			{
				jCheckPaquete = new JCheckBox();
				getContentPane().add(jCheckPaquete);
				jCheckPaquete.setEnabled(false);
				jCheckPaquete.setText("Paquete");
				jCheckPaquete.setBounds(18, 122, 97, 23);
				jCheckPaquete.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jCheckPaquete.actionPerformed, event="+evt);					
						if(jCheckPaquete.isSelected())
				        {
							jComboBPaquete.setEnabled(true);
				        }
				        else jComboBPaquete.setEnabled(false);
					}
				});
				
			}
			{
				ComboBoxModel jComboBServicioDatosModel = 
					new DefaultComboBoxModel(
							new String[] { "" });
				jComboBServicioDatos = new JComboBox();
				getContentPane().add(jComboBServicioDatos);
				jComboBServicioDatos.setModel(jComboBServicioDatosModel);
				jComboBServicioDatos.setEnabled(false);
				jComboBServicioDatos.setOpaque(false);
				jComboBServicioDatos.setBounds(115, 223, 279, 25);
				cargaComboDatos();
			}
			{
				ComboBoxModel jComboBServicioSmsModel = 
					new DefaultComboBoxModel(
							new String[] { "" });
				jComboBServicioSms = new JComboBox();
				getContentPane().add(jComboBServicioSms);
				jComboBServicioSms.setModel(jComboBServicioSmsModel);
				jComboBServicioSms.setEnabled(false);
				jComboBServicioSms.setOpaque(false);
				jComboBServicioSms.setBounds(115, 188, 279, 27);
				cargaComboSms();
			}
			{
				ComboBoxModel jComboBServicioVozModel = 
					new DefaultComboBoxModel(
							new String[] { "" });
				jComboBServicioVoz = new JComboBox();
				getContentPane().add(jComboBServicioVoz);
				jComboBServicioVoz.setModel(jComboBServicioVozModel);
				jComboBServicioVoz.setEnabled(false);
				jComboBServicioVoz.setOpaque(false);
				jComboBServicioVoz.setBounds(115, 154, 279, 25);
				cargaComboVoz();
			}
			{
				ComboBoxModel jComboBox1Model = 
					new DefaultComboBoxModel(
							new String[] { "" });
				jComboBPaquete = new JComboBox();
				getContentPane().add(jComboBPaquete);
				getContentPane().add(getLblCostoExcedente());
				getContentPane().add(getJTCosto());
				jComboBPaquete.setModel(jComboBox1Model);
				jComboBPaquete.setEnabled(false);
				jComboBPaquete.setOpaque(false);
				jComboBPaquete.setBounds(115, 120, 279, 25);
				cargaComboPaquete();
			}
			
			pack();
			this.setSize(571, 345);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public void cargaComboSms ()
	{
		jComboBServicioSms.removeAllItems();
		
		//logica para BBDD//
		//Vector<String> a = AdministradorPersistenciaServico.getInstancia().cargarComboSrv("SMS"); 
			
		Vector<String> a = Sistema.getInstancia().cargaComboSms("SMS");
		for (int i = 0; i < a.size(); i++) 
			{
			 String c = a.elementAt(i);
			 jComboBServicioSms.addItem(c); 								
		    }	
	}
	
	public void cargaComboDatos ()
	{
jComboBServicioDatos.removeAllItems();
		
		//logica para BBDD//
		//Vector<String> a = AdministradorPersistenciaServico.getInstancia().cargarComboSrv("DATOS"); 
			
		Vector<String> a = Sistema.getInstancia().cargaComboDatos("DATOS");
		for (int i = 0; i < a.size(); i++) 
			{
			 String c = a.elementAt(i);
			 jComboBServicioDatos.addItem(c); 								
		    }		
	}
	
	public void cargaComboVoz ()
	{
		jComboBServicioVoz.removeAllItems();
		
		//logica para BBDD//
		//Vector<String> a = AdministradorPersistenciaServico.getInstancia().cargarComboSrv("VOZ"); 
			
		Vector<String> a = Sistema.getInstancia().cargaComboVoz("VOZ");
		for (int i = 0; i < a.size(); i++) 
			{
			 String c = a.elementAt(i);
			 jComboBServicioVoz.addItem(c); 								
		    }	
	}


	public void cargaComboPaquete ()
	{
		jComboBPaquete.removeAllItems();
		
		//logica para BBDD//
		//Vector<String> a = AdministradorPersistenciaServico.getInstancia().cargarComboSrv("VOZ"); 
			
		Vector<String> a = Sistema.getInstancia().cargaComboPaquete("PAQUETE");
		for (int i = 0; i < a.size(); i++) 
			{
			 String c = a.elementAt(i);
			 jComboBPaquete.addItem(c); 								
		    }	
	}
	
	private JLabel getLblCostoExcedente() {
		if(lblCostoExcedente == null) {
			lblCostoExcedente = new JLabel();
			lblCostoExcedente.setText("Costo Exedente");
			lblCostoExcedente.setBounds(18, 96, 81, 16);
		}
		return lblCostoExcedente;
	}
	
	private JTextField getJTCosto() {
		if(jTCosto == null) {
			jTCosto = new JTextField();
			jTCosto.setBounds(120, 93, 81, 23);
			jTCosto.setEnabled(false);
		}
		return jTCosto;
	}

}
